package game;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

class Room {
    private Map<String, Room> exits = new HashMap<>();
    private String name;

    public Room(String name) {
        this.name = name;
    }

    public void setExit(String dir, Room room) {
        exits.put(dir, room);
    }

    public Room getExit(String dir) {
        return exits.get(dir);
    }

    public void printInfo() {
        System.out.printf("你现在在：%s, 出口有：\n", name);
        exits.keySet().forEach(System.out::println);
        System.out.println();
    }
}

/**
 * 城堡游戏
 */
public class CastleGame {
    private Room current;
    private Scanner scanner = new Scanner(System.in);

    private Room[] rooms = new Room[]{
            new Room("大厅"),
            new Room("餐厅"),
            new Room("厕所"),
            new Room("花园"),
    };

    public CastleGame() {
        rooms[0].setExit("east", rooms[2]);
        rooms[2].setExit("west", rooms[0]);
        rooms[0].setExit("west", rooms[1]);
        rooms[1].setExit("east", rooms[0]);
        rooms[2].setExit("north", rooms[3]);
        rooms[3].setExit("south", rooms[2]);

        current = rooms[new Random().nextInt(rooms.length)];
    }

    public void play() {
        while(true) {
            current.printInfo();
            System.out.println("What are you going? or exit?");
            String cmd = scanner.nextLine();
            if("exit".equals(cmd))
                return;

            Room exit;
            if ((exit = current.getExit(cmd)) == null) {
                System.out.println("No way, please input again!");
                continue;
            }
            current = exit;
        }
    }

    public static void main(String[] args) {
        new CastleGame().play();
    }
}
